﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;

/// <summary>
///ApplicationHelper 的摘要说明
/// </summary>
public class ApplicationHelper
{
    #region Methods

    /// <summary>
    /// 向System.Web.UI.Page对象注册启动客户端脚本
    /// </summary>
    /// <param name="page">页面对象</param>
    /// <param name="script">script脚本</param>
    public static void AddStartupScript(Page page, string script)
    {
        Guid guid = Guid.NewGuid();

        string scriptkey = guid.ToString();

        if (page.ClientScript.IsStartupScriptRegistered(page.GetType(), scriptkey))
        {
            AddScriptBlock(page, script);
        }

        page.ClientScript.RegisterStartupScript(page.GetType(), scriptkey, script, true);
    }

    /// <summary>
    /// 以alert信息在客户端显示信息
    /// </summary>
    /// <param name="page">页面对象</param>
    /// <param name="message">消息</param>
    public static void ShowClientMessage(Page page, string message)
    {
        if (string.IsNullOrEmpty(message))
        {
            return;
        }
        //AddScriptBlock(page, string.Format("setTimeout(\"jAlert('{0}','提示窗口');\",0);", message));
        //AddScriptBlock(page, string.Format("jAlert(\"{0}\",\"提示窗口\");", message));
        AddScriptBlock(page, string.Format("alert(\"{0}\",\"提示窗口\");", message));
    }
    public static void ShowParentClinetMessage(Page page, string message)
    {
        AddScriptBlock(page, string.Format("parent.jAlert('{0}','提示窗口')", message));
    }
    public static void CloseParentWindow(Page page)
    {
        ApplicationHelper.AddStartupScript(page, "parent.closeWin();");
    }
    /// <summary>
    /// 向System.Web.UI.Page对象注册客户端脚本
    /// </summary>
    /// <param name="page">页面对象</param>
    /// <param name="script">script脚本</param>
    public static void AddScriptBlock(Page page, string script)
    {
        Guid guid = Guid.NewGuid();

        string scriptkey = guid.ToString();

        if (page.ClientScript.IsClientScriptBlockRegistered(page.GetType(), scriptkey))
        {
            AddScriptBlock(page, script);
        }

        page.ClientScript.RegisterClientScriptBlock(page.GetType(), scriptkey, script, true);
    }

    /// <summary>
    /// 获取被省略掉字符串
    /// </summary>
    /// <param name="input">输入字符串</param>
    /// <param name="length">最多显示的字符长度（汉字代表两个字符），多余的用...代替</param>
    /// <returns></returns>
    public static string GetOmittedString(string input, int length)
    {
        int realLength = 0;
        for (int i = 0; i < input.Length; i++)
        {
            if (input[i] >= 0x4e00 && input[i] <= 0x9fa5)
            {
                realLength += 2;
            }
            else
            {
                realLength++;
            }

            if (realLength == length)
            {
                return input.Substring(0, i + 1) + "..";
            }
            else if (realLength > length)
            {
                return input.Substring(0, i) + "..";
            }
        }

        return input;
    }

    /// <summary>
    /// 移除所有HTML标签
    /// </summary>
    /// <param name="strHtml">要剔除html的内容</param>
    public static string RemoveAllHtmlStript(string strHtml)
    {
        string[] aryReg ={
          @"<script[^>]*?>.*?</script>",
          @"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
          @"([\r\n])[\s]+",
          @"&(quot|#34);",
          @"&(amp|#38);",
          @"&(lt|#60);",
          @"&(gt|#62);", 
          @"&(nbsp|#160);", 
          @"&(iexcl|#161);",
          @"&(cent|#162);",
          @"&(pound|#163);",
          @"&(copy|#169);",
          @"&#(\d+);",
          @"-->",
          @"<!--.*\n"         
         };

        string[] aryRep = {
           "",
           "",
           "",
           "\"",
           "&",
           "<",
           ">",
           " ",
           "\xa1",//chr(161),
           "\xa2",//chr(162),
           "\xa3",//chr(163),
           "\xa9",//chr(169),
           "",
           "\r\n",
           ""
          };

        string newReg = aryReg[0];
        for (int i = 0; i < aryReg.Length; i++)
        {
            Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
            strHtml = regex.Replace(strHtml, aryRep[i]);

        }
        strHtml = strHtml.Replace("<", "");
        strHtml = strHtml.Replace(">", "");
        strHtml = strHtml.Replace("\r\n", "");
        strHtml = strHtml.Trim();
        return strHtml;
    }
    #endregion
}
